package org.opens.javaskill.design;

/**
 * @Date 2020/8/29 13:59
 * @Created by 99126
 */
public class NullObjectPattern {

    public static void main(String[] args) {
        Customer one = CustomerFactory.getInstance("one");
        Customer two = CustomerFactory.getInstance("two");
        Customer three = CustomerFactory.getInstance("three");

        System.out.println(one.getName());
        System.out.println(two.getName());
        System.out.println(three.getName());
    }

}

abstract class Customer {

    protected String name;

    public abstract boolean isNull();

    public String getName() {
        return this.name;
    };

}

class CustomerFactory {

    private CustomerFactory() {
    }

    private static String[] nameArr = {"one", "two"};

    public static Customer getInstance(String name) {
        for (String nameTemp : nameArr) {
            if ( nameTemp.equalsIgnoreCase(name) ) {
                return new RealCustomer(name);
            }
        }
        return new NullCustomer();
    }

}

class RealCustomer extends Customer {

    public RealCustomer(String name) {
        this.name = name;
    }

    @Override
    public boolean isNull() {
        return false;
    }

}

class NullCustomer extends Customer {

    public NullCustomer() {
    }

    @Override
    public boolean isNull() {
        return true;
    }

    @Override
    public String getName() {
        return "the object name filed is null";
    }
}